package com.tundriki.pref;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
import android.opengl.GLUtils;

import javax.microedition.khronos.opengles.GL10;

public class TextureBundle {
	public static int TEX_COUNT=57;
	private int[] texNames = new int[TEX_COUNT];
	public final String[] cardNames;
	public final Context context;
	public final FloatBuffer stdTexMap;
	public final float[] cardShape;
	
	//@SuppressWarnings("static-access")
	public TextureBundle(GL10 gl, Context context) {
		super();
		int i,j,k=0,x,y, texHgt=128, texWid=128;
		this.context = context;
		this.cardNames = this.context.getResources().getStringArray(R.array.textNames);
		this.stdTexMap = bufferFromResources(R.array.simpleTextureMap, 2);
		cardShape = arrayFromResources(R.array.cardShape, 3);
		
		
		Bitmap deck = BitmapFactory.decodeResource(this.context.getResources(), R.drawable.cards);
		Bitmap tmp;
	
		for(i=0;i<texNames.length;i++)
			texNames[i] = -1;
		
		texHgt = deck.getHeight() / 5;
		texWid = deck.getWidth() / 13;

		gl.glGenTextures(texNames.length, texNames, 0);		
		for (i=0, y=0, k=0;i<4;i++, y+=texHgt)
			for(j=0,x=0;j<13;j++,k++,x+=texWid) {
				tmp = Bitmap.createBitmap(deck, x, y, texWid, texHgt);
				gl.glBindTexture(GL10.GL_TEXTURE_2D, texNames[k]);
				gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); 
				gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); 				
				GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, tmp, 0);
				tmp.recycle();
			}
		
		tmp = Bitmap.createBitmap(deck, 0, texHgt*4, texWid, texHgt);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texNames[k++]);
		gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); 
		gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); 				
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, tmp, 0);
		tmp.recycle();
		
	    
		tmp = Bitmap.createBitmap(deck, texWid, texHgt*4, texWid, texHgt);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texNames[k++]);
		gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); 
		gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); 				
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, tmp, 0);
		tmp.recycle();
	    deck.recycle();
	    
	    tmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.desk);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texNames[k++]);
	    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
	    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, tmp, 0); 
	    tmp.recycle();

	    tmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.pool);
		gl.glGenTextures(1, texNames, k);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texNames[k++]);
	    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
	    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, tmp, 0); 
	    tmp.recycle(); 
	}
	
	public int texByName(String name) {
	int i;
		for(i=0;i<TEX_COUNT;i++)
			if (cardNames[i].equalsIgnoreCase(name))
				return texNames[i];
		return -1;
	}
	
	
	public int texByIdx(int idx) {
		if (idx>=0 && idx<texNames.length)
			return texNames[idx];
		return -1;
	}
	
	/* common utils */
	public FloatBuffer bufferFromResources(int resId, int vectLen) {
		return bufferFromResources(this.context, resId, vectLen);
	}
	public float[] arrayFromResources(int resId) {
		return arrayFromResources(this.context, resId, 1);
	}
	public float[] arrayFromResources(int resId, int vectLen) {
		return arrayFromResources(this.context, resId, vectLen);
	}
	
	/* static utils */
	public static float[] arrayFromResources(Context ctx, int resId, int vectLen) {
		String src[] = ctx.getResources().getStringArray(resId);
		float rv[] = new float[src.length*vectLen];
		for(int i=0, l=0;i<src.length;i++) {
			String s = src[i].trim();
			for(int j=0;j<vectLen;j++,l++) {
				int k = s.indexOf(',');
				String token = s;
				if (k<0) 
					s = "";
				else {
					token = s.substring(0,k).trim();
					s = s.substring(k+1).trim();
				}
				try {
					rv[l] = Float.parseFloat(token);
				} catch(Exception e) {
					rv[l] = 0;
				}
			}
		}
		return rv;		
	}
	
	public static float[] arrayFromResources(Context ctx, int resId) {
		return arrayFromResources(ctx, resId, 1);
	}
	
	public static FloatBuffer bufferFromResources(Context ctx, int resId, int vectLen) {
		return createBuffer(arrayFromResources(ctx, resId, vectLen));
	}
	
	public static FloatBuffer createBuffer(int length) {
		ByteBuffer bb = ByteBuffer.allocateDirect(length*4); // 4 === sizeof(float)
		FloatBuffer fb;
		bb.order(ByteOrder.nativeOrder());
		fb = bb.asFloatBuffer();
		fb.position(0);
		return fb;
	}
	
	public static FloatBuffer createBuffer(float[] array) {
		FloatBuffer fb = createBuffer(array.length);
		fb.put(array);
		fb.position(0);
		return fb;
	}

}